package com.wx.controller;

import com.alibaba.fastjson.JSON;
import com.wx.consts.UserConstant;
import com.wx.entity.ResultMessage;
import com.wx.entity.User;
import com.wx.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by Max on 2018/4/13.
 */
@Controller
public class UserController extends BaseController{

    @Resource
    UserService userService;

    /**
     * 跳转到登录页面
     */
    @RequestMapping("login")
    public ModelAndView toLogin(){

        return toView("login");
    }

    /**
     * 进行登录操作
     */
    @RequestMapping("doLogin")
    @ResponseBody
    public String doLogin(String username,String password){
        //判断用户是否存在
        User user = userService.doLogin(username,password);
        if(user!=null){
            getSession().setAttribute(UserConstant.USER_INFO,JSON.toJSONString(user));
        }
        return JSON.toJSONString(new ResultMessage().success());
    }

    @RequestMapping("home")
    public ModelAndView toHome(){
        return toView("home");
    }


}
